<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/js/layui/css/layui.css" media="all">
    <script type="text/javascript" src="/static/js/jquery/jquery-3.5.1.min.js"></script>
    <script type="text/javascript" src="/static/js/layui/layui.js"></script>
</head>
<body>
<div class="layui-tab layui-tab-brief">
    <div class="layui-col-md2 layui-col-md-offset1">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">供货商：</label>
                <div class="layui-input-block">
                    <input type="text" name="title" id="name" style="width: 200px;" lay-verify="title" autocomplete="off" placeholder="请输入供货商名" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">地址：</label>
                <div class="layui-input-inline" style="width: 200px;" id="shengfen">
                    <select name="quiz1" lay-filter="quiz1-filter" id="lselect1">
                        <option value="" selected="">请选择省份</option>
                        <option value="安徽" >安徽</option>
                        <option value="浙江">浙江</option>
                        <option value="江苏">江苏</option>
                    </select>
                </div>
                <div class="layui-input-inline" style="width: 200px;" id="city">
                    <select name="quiz2" lay-filter=" quiz2-filter">
                        <option value="" selected="">请选择城市</option>
                        <option value="杭州">杭州</option>
                        <option value="宁波" disabled="">宁波</option>
                        <option value="温州">温州</option>
                        <option value="温州">台州</option>
                        <option value="温州">绍兴</option>
                    </select>
                </div>
                <label class="layui-form-label">详细地址：</label>
                <div class="layui-input-inline">
                    <input type="text" id="address" name="address" style="width: 200px;" lay-verify="address" autocomplete="off" placeholder="请填写详细地址" class="layui-input">
                </div>
            </div>
            <div style="width: 350px; text-align: center">
                <div class="layui-btn-container">
                    <button type="button" id="btn" class="layui-btn layui-btn-lg layui-btn-radius layui-btn-normal">确认添加</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['element', 'layer', 'util','laydate','form'], function(){
        var $ = layui.$
            ,form = layui.form;
        // 联动选择
        form.on("select(quiz1-filter)",function (){
            console.log("到这了");
            if ($("#shengfen").find("option:selected").val()==""){
                return;
            }else {
                $("#city").empty();
                if($("#shengfen").find("option:selected").val()=='安徽'){
                    $("#city").append('<select name="quiz2" lay-filter="quiz2-filter"><option value="" selected="">请选择城市</option><option value="合肥">合肥</option><option value="阜阳">阜阳</option><option value="芜湖">芜湖</option></select>')
                }else if ($("#shengfen").find("option:selected").val()=='浙江'){
                    $("#city").append('<select name="quiz2" lay-filter="quiz2-filter"><option value="" selected="">请选择城市</option><option value="温州">温州</option><option value="宁波">宁波</option></select>')
                }else if ($("#shengfen").find("option:selected").val()=='江苏'){
                    $("#city").append('<select name="quiz2" lay-filter="quiz2-filter"><option value="" selected="">请选择城市</option><option value="南京">南京</option></select>')
                }else{
                    // 没写
                }
                layui.form.render("select");//重新渲染
                return;
            }
        });
    });
    $(function () {
        $("#btn").click(function(){
            if ($("#name").val()==''){
                layer.msg("请输入供货商名称");
                return;
            }
            if ($("#shengfen").find("option:selected").text()=="直接选择或搜索"){
                layer.msg('请选择地址信息！');
                return;
            }
            if ($("#city").find("option:selected").text()=="直接选择或搜索"){
                layer.msg('请选择地址信息！');
                return;
            }
            if (!($("#address").val())){
                layer.msg('请填写详细地址！');
                return;
            }
            var param = {"name":$("#name").val(),"address":$("#shengfen").find("option:selected").val()+$("#city").find("option:selected").val()+$("#address").val()}
            console.log(param);
            $.ajax({
                url:"/main/manage/insert/merchant",
                data:param,
                dataType:"json",
                success:function (data){
                    // console.log(data);
                    if (data==1){
                        layer.msg("添加成功");
                        setTimeout(function (){
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index);
                        },1500);
                    }else if (data==2){
                        layer.msg("商品已存在");
                    }else {
                        layer.msg("发生什么事了！！");
                    }
                }
            })
        });
    });
</script>
</body>
</html>